home *** CD-ROM | disk | FTP | other *** search
/ Windows Expert / Windows Expert.iso / others / ole_101.zip / PATRON.ZIP / INIT.C < prev    next >
C/C++ Source or Header  |  1992-04-13  |  7KB  |  240 lines

  1. /*
  2.  * INIT.C
  3.  *
  4.  * Application (not OLE) specific initialization code.
  5.  *
  6.  * Copyright(c) Microsoft Corp. 1992 All Rights Reserved
  7.  *
  8.  */
  9.  
  10. #include <windows.h>
  11. #include <ole.h>
  12. #include "oclient.h"
  13. #include "blackbox.h"
  14. #include "patron.h"
  15.  
  16.  
  17.  
  18. /*
  19.  * FApplicationInit
  20.  *
  21.  * Purpose:
  22.  *  All application specific initialization including loading
  23.  *  the stringtable, registering window classes, and calling any
  24.  *  OLE specific initialziation code.
  25.  *
  26.  * Parameters:
  27.  *  pGlob           LPGLOBALS to global variable block.
  28.  *  hPrevInstance   HANDLE to the previous application instance, if any.
  29.  *
  30.  * Return Value:
  31.  *  BOOL            TRUE if everything succeeds, FALSE otherwise.
  32.  *                  If FALSE is returned, allocated memory and objects
  33.  *                  are not necessarily freed.  The caller should then
  34.  *                  use the FApplicationExit function to perform cleanup.
  35.  */
  36.  
  37. BOOL FAR PASCAL FApplicationInit(LPGLOBALS pGlob, HANDLE hPrevInstance)
  38.     {
  39.     LPSTR           psz;
  40.     LPSTR           pszT;
  41.     BOOL            fTemp;  //For OLE, PDocumentAllocate
  42.  
  43.     /*
  44.      * InitApp allocates local memory for strings. WinMain must free.
  45.      * If this fails, we should quit BEFORE we register any classes
  46.      * or do anything else to suck up USER or GDI resources.
  47.      */
  48.     pGlob->hMemStrings=HLoadAppStrings(pGlob->hInst);
  49.  
  50.     if (NULL==pGlob->hMemStrings)
  51.         return FALSE;
  52.  
  53.     //Classes are only registered if hPrevInstance is NULL.
  54.     if (!FClassRegister(pGlob, hPrevInstance))
  55.         {
  56.         LocalFree(pGlob->hMemStrings);
  57.         return FALSE;
  58.         }
  59.  
  60.     /*
  61.      * If there is something on the command line, copy it to the szFile
  62.      * buffer in pGlob.
  63.      */
  64.  
  65.     psz=pGlob->pszCmdLine;
  66.  
  67.     //Skip whitespace to the beginning of the first item in the command line.
  68.     psz=PszWhiteSpaceScan(psz, TRUE);
  69.  
  70.     if (0!=*psz)
  71.         {
  72.         /*
  73.          * If there's something here, find the end (next whitespace) and
  74.          * null-terminate it.  The copy it into the filename buffer.
  75.          */
  76.         pszT=PszWhiteSpaceScan(psz, FALSE);
  77.         *pszT=0;
  78.  
  79.         lstrcpy(pGlob->szFile, psz);
  80.         }
  81.  
  82.  
  83.     /*
  84.      * OLE:  Create a DOCUMENT structure.  This has the effect of
  85.      * initializing VTBLs and allocating other structures.  Note that
  86.      * we do not register any documents until later when we atually
  87.      * open one.  Note that we pass ClientCallback so the constructor
  88.      * can put it in the OLECLIENTVTBL and the default StreamGet and
  89.      * StreamPut methods (OLESTREA.C) so the constructor can put them
  90.      * in the OLESTREAMVTBL referenced through this document.
  91.      */
  92.  
  93.     pDoc=PDocumentAllocate(&fTemp, pGlob->hInst, rgpsz[IDS_CAPTION],
  94.                            (FARPROC)ClientCallback, (FARPROC)StreamGet,
  95.                            (FARPROC)StreamPut);
  96.  
  97.  
  98.     //If fTemp is FALSE, we'll call FApplicationExit which frees pDoc.
  99.     return fTemp;
  100.     }
  101.  
  102.  
  103.  
  104.  
  105. /*
  106.  * FClassRegister
  107.  *
  108.  * Purpose:
  109.  *  Registers classes used by the application:  "Patron" the main
  110.  *  window.
  111.  *
  112.  * Parameters:
  113.  *  pGlob           LPGLOBALS to the global variables block.
  114.  *  hPrevInstance   HANDLE of any previous application instance.
  115.  *
  116.  * Return Value:
  117.  *  BOOL            TRUE if all classes are successfully registered
  118.  *                  (or if hPrevInstance is non-NULL).  FALSE is
  119.  *                  any registration fails.
  120.  *
  121.  */
  122.  
  123. BOOL FAR PASCAL FClassRegister(LPGLOBALS pGlob, HANDLE hPrevInstance)
  124.     {
  125.     WNDCLASS        wc;
  126.  
  127.     if (hPrevInstance)
  128.         return TRUE;
  129.  
  130.     /*
  131.      * Note that we do not need to unregister classes on a failure
  132.      * since that's part of automatic app cleanup.
  133.      */
  134.     wc.style         = CS_HREDRAW | CS_VREDRAW;
  135.     wc.lpfnWndProc   = PatronWndProc;
  136.     wc.cbClsExtra    = 0;
  137.     wc.cbWndExtra    = 0;
  138.     wc.hInstance     = pGlob->hInst;
  139.     wc.hIcon         = LoadIcon(pGlob->hInst, "Icon");
  140.     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  141.     wc.hbrBackground = COLOR_APPWORKSPACE + 1;
  142.     wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MENU);
  143.     wc.lpszClassName = rgpsz[IDS_CLASSPATRON];
  144.  
  145.     if (!RegisterClass(&wc))
  146.         return FALSE;
  147.  
  148.  
  149.     wc.style         = CS_DBLCLKS | CS_HREDRAW | CS_VREDRAW;
  150.     wc.lpfnWndProc   = BlackBoxWndProc;
  151.     wc.cbClsExtra    = 0;
  152.     wc.cbWndExtra    = CBWNDEXTRABLACKBOX;
  153.     wc.hInstance     = pGlob->hInst;
  154.     wc.hIcon         = NULL;
  155.     wc.hCursor       = LoadCursor(NULL, IDC_CROSS);
  156.     wc.hbrBackground = COLOR_WINDOW + 1;
  157.     wc.lpszMenuName  = NULL;
  158.     wc.lpszClassName = rgpsz[IDS_CLASSBLACKBOX];
  159.  
  160.     if (!RegisterClass(&wc))
  161.         return FALSE;
  162.  
  163.     return TRUE;
  164.     }
  165.  
  166.  
  167.  
  168.  
  169. /*
  170.  * HLoadAppStrings
  171.  *
  172.  * Purpose:
  173.  *  Allocates FIXED local memory and reads the applications
  174.  *  string resources into that memory.  Each string's pointer
  175.  *  is available with rgpsz[i] where i is the ID value of the
  176.  *  string.  The strings must have sequential IDs.
  177.  *
  178.  * Parameters:
  179.  *  hInst           HANDLE to the application instance.
  180.  *
  181.  * Return Value:
  182.  *  HANDLE          Handle to the local memory.  NULL if memory could
  183.  *                  not be allocated.
  184.  */
  185.  
  186. HANDLE FAR PASCAL HLoadAppStrings(HANDLE hInst)
  187.     {
  188.     HANDLE      hMem;
  189.     char NEAR   *pch;
  190.     WORD        cchUsed=0;
  191.     WORD        cch;
  192.     short       i;
  193.  
  194.     /*
  195.      * Allocate memory and load strings.  NOTE!  The LPTR style
  196.      * specifies FIXED memory.  This should not be a big deal
  197.      * since this is an early allocation into the local heap.
  198.      * But it should be watched if the number of strings becomes
  199.      * large.
  200.      */
  201.     hMem=LocalAlloc(LPTR, CSTRINGS*CCHSTRINGMAX);
  202.  
  203.     if (hMem==NULL)
  204.         return (HANDLE)NULL;
  205.  
  206.     /*
  207.      * This operation is only valid for FIXED memory.  Otherwise use
  208.      * LocalLock.
  209.      */
  210.     pch=(char *)hMem;
  211.  
  212.  
  213.     /*
  214.      * Load the strings into the memory and retain the specific
  215.      * pointer to that string.
  216.      */
  217.     for (i=0; i<CSTRINGS; i++)
  218.         {
  219.         cch=LoadString(hInst, i, (LPSTR)(pch+cchUsed), CCHSTRINGMAX-1);
  220.         rgpsz[i]=(char *)(pch+cchUsed);
  221.  
  222.         /*
  223.          * One is added to cch to include a NULL.  The memory was ZEROINITed
  224.          * on allocation so by skipping a byte we get the NULL.
  225.          */
  226.         cchUsed +=cch+1;
  227.         }
  228.  
  229.     /*
  230.      * We are assuming that no string is over CCHSTRINGMAX, and therefore
  231.      * we did not use all the allocated memory.  Therefore LocalReAlloc
  232.      * will only SHRINK the block, never expand it.  So if it fails, we
  233.      * don't care--all the strings are still there, we just wasted some
  234.      * space.
  235.      */
  236.     LocalReAlloc(hMem, cchUsed+1, LPTR);
  237.  
  238.     return hMem;
  239.     }
  240.